﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.SlotJoint.CApSlotJointUtils
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;
using System.Linq;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.SlotJoint;

public static class CApSlotJointUtils
{
  public static double EPSILON = 0.01;

  public static bool IsPoint1CloserToCurveStartOrEndPoint(
    TxVector p1,
    TxVector p2,
    ITx1DimensionalGeometry curve,
    bool checkCurveStartPoint)
  {
    bool curveStartOrEndPoint = false;
    if (TxVector.op_Inequality(p1, (TxVector) null) && TxVector.op_Inequality(p2, (TxVector) null) && curve != null)
    {
      TxVector p2_1 = checkCurveStartPoint ? curve.ApproximatedStartPoint : curve.ApproximatedEndPoint;
      if (TxVector.op_Inequality(p2_1, (TxVector) null) && CApSlotJointUtils.CalculateDistance(p1, p2_1) < CApSlotJointUtils.CalculateDistance(p2, p2_1))
        curveStartOrEndPoint = true;
    }
    return curveStartOrEndPoint;
  }

  public static bool ArePointsIdentical(TxVector pin1, TxVector pin2)
  {
    bool flag = false;
    if (TxVector.op_Inequality(pin1, (TxVector) null) && TxVector.op_Inequality(pin2, (TxVector) null) && CApSlotJointUtils.CalculateDistance(pin1, pin2) < CApSlotJointUtils.EPSILON)
      flag = true;
    return flag;
  }

  public static bool IsCurveValid(ITx1DimensionalGeometry curve)
  {
    bool flag = false;
    if (curve != null)
    {
      TxApproximationData approximation = ((ITxGeometry) curve).Approximation;
      if (approximation != null && approximation.Points != null && approximation.Points.Length != 0)
        flag = true;
    }
    return flag;
  }

  public static bool IsCurveLine(ITx1DimensionalGeometry curve)
  {
    bool flag = false;
    if (curve != null)
    {
      TxApproximationData approximation = ((ITxGeometry) curve).Approximation;
      if (approximation != null && approximation.Points != null && approximation.Points.Length > 1)
      {
        TxLineData txLineData = new TxLineData(approximation.Points[0], approximation.Points[approximation.Points.Length - 1]);
        flag = true;
        foreach (TxVector point in approximation.Points)
        {
          double num;
          TxParameterizedPoint parameterizedPoint;
          txLineData.GetPointProjection(point, ref num, ref parameterizedPoint);
          if (num > CApSlotJointUtils.EPSILON)
          {
            flag = false;
            break;
          }
        }
      }
    }
    return flag;
  }

  public static bool IsCurveClosed(ITx1DimensionalGeometry curve)
  {
    bool flag = false;
    if (curve != null)
    {
      TxVector approximatedStartPoint = curve.ApproximatedStartPoint;
      TxVector approximatedEndPoint = curve.ApproximatedEndPoint;
      if (TxVector.op_Inequality(approximatedStartPoint, (TxVector) null) && TxVector.op_Inequality(approximatedEndPoint, (TxVector) null) && CApSlotJointUtils.CalculateDistance(approximatedStartPoint, approximatedEndPoint) < CApSlotJointUtils.EPSILON)
        flag = true;
    }
    return flag;
  }

  public static bool IsPointOnPlaneOfCurve(TxVector point, ITx1DimensionalGeometry curve)
  {
    bool flag = false;
    TxPlaneData curvePlaneData = CApSlotJointUtils.GetCurvePlaneData(curve);
    if (TxPlaneData.op_Inequality(curvePlaneData, (TxPlaneData) null))
      flag = CApSlotJointUtils.IsPointOnPlane(curvePlaneData, point);
    return flag;
  }

  public static bool IsPointOnPlaneOfCurves(
    TxVector point,
    ITx1DimensionalGeometry curve1,
    ITx1DimensionalGeometry curve2)
  {
    bool flag = false;
    TxPlaneData curvesPlaneData = CApSlotJointUtils.GetCurvesPlaneData(curve1, curve2);
    if (TxPlaneData.op_Inequality(curvesPlaneData, (TxPlaneData) null))
      flag = CApSlotJointUtils.IsPointOnPlane(curvesPlaneData, point);
    return flag;
  }

  public static TxPlaneData GetCurvePlaneData(ITx1DimensionalGeometry curve)
  {
    TxPlaneData curvePlaneData = (TxPlaneData) null;
    try
    {
      if (curve != null)
      {
        if (((ITxGeometry) curve).Approximation != null)
        {
          List<TxVector> list = ((IEnumerable<TxVector>) ((ITxGeometry) curve).Approximation.Points).ToList<TxVector>();
          if (list.Count > 2)
          {
            TxVector[] txVectorArray = CApSlotJointUtils.Get3PointsThatCanFormPlane(list);
            if (TxVector.op_Inequality(txVectorArray[0], (TxVector) null))
            {
              if (TxVector.op_Inequality(txVectorArray[1], (TxVector) null))
              {
                if (TxVector.op_Inequality(txVectorArray[2], (TxVector) null))
                {
                  ITxLocatableObject itxLocatableObject = curve as ITxLocatableObject;
                  curvePlaneData = new TxPlaneData(itxLocatableObject.AbsoluteLocation.Transform(txVectorArray[0]), itxLocatableObject.AbsoluteLocation.Transform(txVectorArray[1]), itxLocatableObject.AbsoluteLocation.Transform(txVectorArray[2]));
                }
              }
            }
          }
        }
      }
    }
    catch (Exception ex)
    {
    }
    return curvePlaneData;
  }

  public static TxPlaneData GetCurvesPlaneData(
    ITx1DimensionalGeometry curve1,
    ITx1DimensionalGeometry curve2)
  {
    TxPlaneData curvesPlaneData = (TxPlaneData) null;
    try
    {
      List<TxVector> points = new List<TxVector>();
      if (curve1 != null && ((ITxGeometry) curve1).Approximation != null)
      {
        ITxLocatableObject itxLocatableObject = curve1 as ITxLocatableObject;
        foreach (TxVector point in ((ITxGeometry) curve1).Approximation.Points)
          points.Add(itxLocatableObject.AbsoluteLocation.Transform(point));
      }
      if (curve2 != null && ((ITxGeometry) curve2).Approximation != null)
      {
        ITxLocatableObject itxLocatableObject = curve2 as ITxLocatableObject;
        foreach (TxVector point in ((ITxGeometry) curve2).Approximation.Points)
          points.Add(itxLocatableObject.AbsoluteLocation.Transform(point));
      }
      if (points.Count > 2)
      {
        TxVector[] txVectorArray = CApSlotJointUtils.Get3PointsThatCanFormPlane(points);
        if (TxVector.op_Inequality(txVectorArray[0], (TxVector) null))
        {
          if (TxVector.op_Inequality(txVectorArray[1], (TxVector) null))
          {
            if (TxVector.op_Inequality(txVectorArray[2], (TxVector) null))
              curvesPlaneData = new TxPlaneData(txVectorArray[0], txVectorArray[1], txVectorArray[2]);
          }
        }
      }
    }
    catch (Exception ex)
    {
    }
    return curvesPlaneData;
  }

  public static bool IsPointOnCurveEndSegment(
    TxVector point,
    ITx1DimensionalGeometry curve,
    out bool isOnFirstSegment)
  {
    bool flag = false;
    isOnFirstSegment = true;
    if (curve != null && ((ITxGeometry) curve).Approximation != null)
    {
      TxVector[] points = ((ITxGeometry) curve).Approximation.Points;
      if (points != null && points.Length > 1)
      {
        ITxLocatableObject itxLocatableObject = curve as ITxLocatableObject;
        TxVector linePoint1_1 = itxLocatableObject.AbsoluteLocation.Transform(points[0]);
        TxVector linePoint2_1 = itxLocatableObject.AbsoluteLocation.Transform(points[1]);
        flag = CApSlotJointUtils.IsPointOnLine(point, linePoint1_1, linePoint2_1);
        if (!flag)
        {
          TxVector linePoint1_2 = itxLocatableObject.AbsoluteLocation.Transform(points[((IEnumerable<TxVector>) points).Count<TxVector>() - 1]);
          TxVector linePoint2_2 = itxLocatableObject.AbsoluteLocation.Transform(points[((IEnumerable<TxVector>) points).Count<TxVector>() - 2]);
          flag = CApSlotJointUtils.IsPointOnLine(point, linePoint1_2, linePoint2_2);
          isOnFirstSegment = false;
        }
      }
    }
    return flag;
  }

  public static double CalculateDistance(TxVector p1, TxVector p2)
  {
    TxVector txVector = TxVector.op_Subtraction(p1, p2);
    return Math.Sqrt(TxVector.DotProduct(txVector, txVector));
  }

  public static bool IsCurvePlanar(ITx1DimensionalGeometry curve)
  {
    bool flag = false;
    try
    {
      if (curve != null)
      {
        TxApproximationData approximation = ((ITxGeometry) curve).Approximation;
        if (((IEnumerable<TxVector>) approximation.Points).Count<TxVector>() > 0 && ((IEnumerable<TxVector>) approximation.Points).Count<TxVector>() < 4)
          flag = true;
        else if (CApSlotJointUtils.IsCurveLine(curve))
        {
          flag = true;
        }
        else
        {
          TxPlaneData curvePlaneData = CApSlotJointUtils.GetCurvePlaneData(curve);
          if (TxPlaneData.op_Inequality(curvePlaneData, (TxPlaneData) null))
          {
            ITxLocatableObject itxLocatableObject = curve as ITxLocatableObject;
            flag = true;
            foreach (TxVector point1 in approximation.Points)
            {
              TxVector point2 = itxLocatableObject.AbsoluteLocation.Transform(point1);
              if (!CApSlotJointUtils.IsPointOnPlane(curvePlaneData, point2))
              {
                flag = false;
                break;
              }
            }
          }
        }
      }
    }
    catch (Exception ex)
    {
      flag = false;
    }
    return flag;
  }

  public static bool IsPointOnPlane(TxPlaneData planeData, TxVector point)
  {
    return Math.Abs(planeData.A * point.X + planeData.B * point.Y + planeData.C * point.Z + planeData.D) <= CApSlotJointUtils.EPSILON;
  }

  private static TxVector[] Get3PointsThatCanFormPlane(List<TxVector> points)
  {
    TxVector[] txVectorArray = new TxVector[3];
    if (points.Count > 2)
    {
      foreach (TxVector point in points)
      {
        if (TxVector.op_Inequality(point, (TxVector) null))
        {
          if (TxVector.op_Equality(txVectorArray[0], (TxVector) null))
            txVectorArray[0] = point;
          else if (TxVector.op_Equality(txVectorArray[1], (TxVector) null))
          {
            if (CApSlotJointUtils.CalculateDistance(point, txVectorArray[0]) > CApSlotJointUtils.EPSILON)
              txVectorArray[1] = point;
          }
          else if (TxVector.op_Equality(txVectorArray[2], (TxVector) null))
          {
            if (CApSlotJointUtils.CanCreatePlane(txVectorArray[0], txVectorArray[1], point))
            {
              double num;
              TxParameterizedPoint parameterizedPoint;
              new TxLineData(txVectorArray[0], txVectorArray[1]).GetPointProjection(point, ref num, ref parameterizedPoint);
              if (num > CApSlotJointUtils.EPSILON)
                txVectorArray[2] = point;
            }
          }
          else
            break;
        }
      }
    }
    return txVectorArray;
  }

  private static bool CanCreatePlane(
    TxVector point1,
    TxVector point2,
    TxVector point3,
    double epsilon = 0.001)
  {
    bool plane = false;
    if (CApSlotJointUtils.CalculateDistance(point1, point3) > CApSlotJointUtils.EPSILON && CApSlotJointUtils.CalculateDistance(point1, point3) > CApSlotJointUtils.EPSILON)
    {
      TxVector txVector1 = TxVector.op_Subtraction(point2, point1);
      TxVector txVector2 = TxVector.op_Subtraction(point3, point1);
      txVector1.Normalize();
      txVector2.Normalize();
      TxVector txVector3 = TxVector.CrossProduct(txVector1, txVector2);
      if (TxVector.op_Multiply(txVector3, txVector3) > epsilon)
        plane = true;
    }
    return plane;
  }

  private static bool IsPointOnLine(TxVector point, TxVector linePoint1, TxVector linePoint2)
  {
    bool flag = false;
    try
    {
      TxLineData txLineData = new TxLineData(linePoint1, linePoint2);
      if (txLineData != null)
      {
        double num;
        TxParameterizedPoint parameterizedPoint;
        txLineData.GetPointProjection(point, ref num, ref parameterizedPoint);
        if (num < CApSlotJointUtils.EPSILON)
        {
          if (parameterizedPoint != null)
          {
            if (parameterizedPoint.TParam >= 0.0 && parameterizedPoint.TParam <= 1.0)
              flag = true;
            else if (CApSlotJointUtils.CalculateDistance(parameterizedPoint.TParam >= 0.5 ? linePoint2 : linePoint1, parameterizedPoint.Point) < CApSlotJointUtils.EPSILON)
              flag = true;
          }
        }
      }
    }
    catch (Exception ex)
    {
    }
    return flag;
  }

  public static bool IsPointOnCurve(TxVector point, ITx1DimensionalGeometry curve)
  {
    bool flag = false;
    if (curve != null)
    {
      TxApproximationData approximation = ((ITxGeometry) curve).Approximation;
      if (approximation != null && approximation.Points != null && approximation.Points.Length > 1)
      {
        TxVector[] points = approximation.Points;
        ITxLocatableObject itxLocatableObject = curve as ITxLocatableObject;
        TxVector linePoint1 = itxLocatableObject.AbsoluteLocation.Transform(points[0]);
        for (int index = 0; index < points.Length - 1; ++index)
        {
          TxVector linePoint2 = itxLocatableObject.AbsoluteLocation.Transform(points[index + 1]);
          if (CApSlotJointUtils.IsPointOnLine(point, linePoint1, linePoint2))
          {
            flag = true;
            break;
          }
          linePoint1 = linePoint2;
        }
      }
    }
    return flag;
  }

  public static bool AreCurvesOnSamePlane(
    ITx1DimensionalGeometry curve,
    ITx1DimensionalGeometry otherCurve)
  {
    bool flag = false;
    if (curve != null && otherCurve != null)
    {
      TxPlaneData curvesPlaneData = CApSlotJointUtils.GetCurvesPlaneData(curve, otherCurve);
      if (TxPlaneData.op_Inequality(curvesPlaneData, (TxPlaneData) null))
      {
        List<TxVector> txVectorList = new List<TxVector>();
        TxApproximationData approximation1 = ((ITxGeometry) curve).Approximation;
        if (approximation1 != null && approximation1.Points != null)
        {
          ITxLocatableObject itxLocatableObject = curve as ITxLocatableObject;
          foreach (TxVector point in approximation1.Points)
            txVectorList.Add(itxLocatableObject.AbsoluteLocation.Transform(point));
        }
        TxApproximationData approximation2 = ((ITxGeometry) otherCurve).Approximation;
        if (approximation2 != null && approximation2.Points != null)
        {
          ITxLocatableObject itxLocatableObject = otherCurve as ITxLocatableObject;
          foreach (TxVector point in approximation2.Points)
            txVectorList.Add(itxLocatableObject.AbsoluteLocation.Transform(point));
        }
        if (txVectorList.Count > 0)
        {
          flag = true;
          foreach (TxVector point in txVectorList)
          {
            if (!CApSlotJointUtils.IsPointOnPlane(curvesPlaneData, point))
            {
              flag = false;
              break;
            }
          }
        }
      }
    }
    return flag;
  }

  public static object curvesPlaneData { get; set; }
}
